package LeetCode.leetcode100;
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public static void main(String[] args) {
        TreeNode p = new TreeNode(10);
        TreeNode p1 = new TreeNode(5);
        TreeNode p2 = new TreeNode(15);
        p.left = p1;
        p.right = p2;

        TreeNode q = new TreeNode(10);
        TreeNode q1 = new TreeNode(5);
        TreeNode q2 = new TreeNode(15);
        q.left = q1;
        q1.right = q2;
        System.out.println(isSameTree(p,q));
    }
    public static boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null)
            return true;
        else if (p == null && q != null)
            return false;
        else if (q == null && p != null)
            return false;
        else {
            if (p.val != q.val)
                return false;
            else{
                boolean left = isSameTree(p.left,q.left);
                if (!left)
                    return false;
                boolean right = isSameTree(p.right,q.right);
                if (!right)
                    return false;
            }
        }
        return true;
    }
}
